package drongo.desktop.undo
{
	import drongo.desktop.undo.shortKey.ShortKeySystem;
	
	import flash.display.Stage;
	import flash.utils.Dictionary;

	/**
	 * 历史操作记录系统
	 * @author 蝈蝈
	 */	
	public class UndoSystem
	{
		/**
		 * 所属容器
		 */
		private var context:*;
		
		
		/**
		 * 快捷键
		 */
		private var shortkey:ShortKeySystem;
		
		public function UndoSystem()
		{
			
		}
		
		public function startup(stage:Stage,content:*):void{
			this.context=content;
			operationMap=new Dictionary();
			shortkey=new ShortKeySystem(stage,shortKeyHandler);
		}
		/**
		 * 按键响应
		 * @param op
		 */
		private function shortKeyHandler(op:String):void
		{
			executeOperation(op);
		}
		//================================================注册操作处理器========================================//
		private var operationMap:Dictionary
		public function registerOperation(key:String,operationClass:Class):void{
			if(operationMap[key]!=null){
				throw new Error("重复注册："+key);
			}
			operationMap[key]=operationClass;
		}
		public function unregisterOperation(key:String):void{
			delete operationMap[key];
		}
		public function executeOperation(key:String,data:Object=null):void{
			var operation:IOperation=getOperation(key);
			if(!operation){
				throw new Error("未找到："+key+" 相关处理器！");
			}
			operation.data=data;
			operation.context=this.context;
			operation.execute();
		}
		
		protected function getOperation(key):IOperation{
			var clazz:Class=operationMap[key];
			if(!clazz){
				throw new Error("未找到："+key+" 相关处理器！");
			}
			return new clazz();
		}
		//================================================注册快捷键========================================//
		/**
		 * 注册快捷键所触发的条件与操作
		 * @param operate
		 * @param keyCodes
		 * @param focusTarget
		 * @param info
		 */
		public function registerShortKey(operate:String,keyCodes:Array,focusTarget:String=null,info:String=null):void{
			shortkey.register(operate,keyCodes,focusTarget,info);
		}
		/**
		 * 取消一个快捷键注册 
		 * @param operate
		 * @param keyCodes
		 * @param focusTarget
		 */
		public function unRegisterShortKey(operate:String,keyCodes:Array,focusTarget:String=null):void{
			shortkey.unregister(operate,keyCodes,focusTarget);
		}
		public function getShotKeyToString(operate:String,focusTarget:String=null):String{
			return shortkey.getOperaterShortcurtToString(operate,focusTarget);
		}
		//================================================设置焦点目标========================================//
		public function get focusTarget():String { 
			return shortkey.focusTarget;
		}
		public function set focusTarget(value:String):void
		{
			shortkey.focusTarget=value;
		}
	}
}